
#include "FunctionData.h"
boolean iGame = true;
int delayTime=0;
unsigned char ThreeArry[16]= {0xcf,0xcf,0xcf,0xcf,0xcf,0xcf,0xcf,0xcf,0xcf,0xcf,0xcf,0xcf,0xcf,0xcf,0xcf,0xcf};
unsigned char TwoArry[16] = {0xdb,0xdb,0xdb,0xdb,0xdb,0xdb,0xdb,0xdb,0xdb,0xdb,0xdb,0xdb,0xdb,0xdb,0xdb,0xdb};
unsigned char OneArry[16] = {0x86,0x86,0x86,0x86,0x86,0x86,0x86,0x86,0x86,0x86,0x86,0x86,0x86,0x86,0x86,0x86};
boolean startMouseConfig = true;
// boolean mouseGame = false;
void setup() 
{
	Config(true);
	Serial.println("V1.0.1");
}

void loop() 
{
	if(offAll == 1)
	{
		Config(true);
		offAll = 0;
		detachInterrupt(digitalPinToInterrupt(MULTIFUNCTION));
		pinMode(MULTIFUNCTION, INPUT_PULLUP);
	}
	autoPowerOff();
	unsigned char charge = digitalRead(CHARGERSTATE);
	unsigned char key = digitalRead(MULTIFUNCTION);

	//changeBrightness()

	//检测到USB插入
	if(stateCHR == 1 && digitalRead(CHARGERSTATE) == 0){
		play(USBPLUGIN);
	}
	//按键蓄力 到达一定时间切换游戏 否则重新开始
	if(digitalRead(MULTIFUNCTION)==0){
	    delayTime++;
	    Serial.println(delayTime);
	}
	if(delayTime==300){
	    play(0x07);
	    showLed(10,0);
	}
	//检测到按键按下；松手后触发
	if(stateBTN==0&&digitalRead(MULTIFUNCTION)==1 && delayTime<=100)
	{
		delayTime = 0;
		isGen = true;
		mouse = 1;
		live = true;
		lost = false;
		galoble_frequen = 1000;
		galoble_gone_us = 700;
		galoble_score = 3;
		play(SWITCHTOPIC);
		delay(500);
		suduNum++;
		state=0;
		runOnce = true;
		startMouseConfig = true;
	}
	if(stateBTN==0&&digitalRead(MULTIFUNCTION)==1 && delayTime>=300)// && (millis()-lastTime>=2000))
	{
		delayTime = 0;

		iGame = !iGame;

		suduNum++;
		state=0;
		runOnce = true;

		isGen = true;
		mouse = 1;
		live = true;
		lost = false;
		galoble_frequen = 1000;
		galoble_gone_us = 700;
		galoble_score = 3;
		startMouseConfig = true;
		AIP1629_WriteBlock(CMD_AIP1629_STARTADDR(0),16,DspBuf);
		delay(1000);
		//sudukuGame = !mouseGame;
	}
	AIP1629_ReadKey(4,KeyBuf); //读按键
	changeBrightness();//改变亮度
	changeVolume();
	if(iGame)
	{
		if(startMouseConfig == true)
		{
			AIP1629_WriteBlock(CMD_AIP1629_STARTADDR(0),16,ThreeArry);
			delay(700);
			AIP1629_WriteBlock(CMD_AIP1629_STARTADDR(0),16,TwoArry);
			delay(700);
			AIP1629_WriteBlock(CMD_AIP1629_STARTADDR(0),16,OneArry);
			delay(700);
		    startMouseConfig = false;
		}
	   	beatMouse();
		beatMouseWin(); 
	}


	if(!iGame)
	{
	  
	    playSuduku(suduNum);
		if(checkWin(suduNum))
		{
			play(READY2NEXT);

			Serial.print("Stop");
			Serial.println(sudukuTimeStop);
			int timeSec = (sudukuTimeStop-sudukuTimeBegin)/1000;
			Serial.println(timeSec);
			int ledNumber = (130-timeSec)/10;
			if(ledNumber<=0){
				ledNumber=1;
			}
			if(ledNumber>=10){
				ledNumber= 10;
			}
			for(int i=1; i<=ledNumber; i++){
				showLed(i,true);
				delay(200);  
			}

			char2HexSeg(finalResult,PCODE);
			AIP1629_WriteBlock(0xC0,16,segConvered);
			delay(1000);	   
			showLed(10,false);
			suduNum=suduNum+1;

			if(suduNum>=119){
				suduNum==0;
			}
			runOnce = true;
		}  
	}
	

	stateBTN = key;
	stateCHR = charge;
	lastKey1 = KeyBuf[0];
	lastKey2 = KeyBuf[3];
}
